<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;

class AdminTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->createAdmin();
        $this->createRole();
        $this->createMenus();
    }

    public function createAdmin()
    {
        \App\Models\Admin::truncate();
        $data  = [
            'user_name' => 'admin',
            'real_name' => '超级管理员',
            'avatar'    => "http://localhost" . config('common.admin_avatar'),
            'mobile'    => '18516634528',
            'pass_word' => \App\Services\Common\AdminService::syncPassWord("123456"),
            'role_id'   => 1,
            'is_enable' => 1,
            'is_delete' => 0,
        ];
        $admin = \App\Models\Admin::insertGetId($data);
    }

    public function createRole()
    {
        \App\Models\AdminRole::truncate();
        $data = [
            'name'       => '超级管理员',
            'is_super'   => 1,
            'menu_ids'   => '[]',
            'menu_texts' => '[]',
            'is_delete'  => 0,
        ];
        $role = \App\Models\AdminRole::create($data);
    }

    public function createMenus()
    {
        $data = [
            [
                'name'     => '权限管理', 'menu_no' => 'permission', 'icon' => 'layui-icon-vercode', 'type' => 1, 'sort' => 1, 'uri' => '',
                'children' => [
                    [
                        'name'     => '账号管理', 'icon' => '', 'type' => 1, 'sort' => 1, 'uri' => '/account/index', 'menu_no' => 'account-index',
                        'children' => [
                            ['name' => '查询', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-search', 'type' => 2, 'btn_type' => 2, 'sort' => 1, 'uri' => '/account/list', 'menu_no' => 'account-query'],
                            ['name' => '新增', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-add-1', 'type' => 2, 'btn_type' => 4, 'sort' => 2, 'uri' => '/account/editAdd', 'menu_no' => 'account-add'],
                            ['name' => '新增保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 3, 'uri' => '/account/create', 'menu_no' => 'account-add-save'],
                            ['name' => '编辑', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-edit', 'type' => 2, 'btn_type' => 4, 'sort' => 3, 'uri' => '/account/editAdd', 'menu_no' => 'account-edit'],
                            ['name' => '编辑保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 4, 'uri' => '/account/update', 'menu_no' => 'account-edit-save'],
                            ['name' => '删除', 'class_name' => 'layui-btn-danger', 'icon' => 'layui-icon-delete', 'type' => 2, 'btn_type' => 1, 'sort' => 5, 'uri' => '/account/deletes', 'menu_no' => 'account-delete'],
                            ['name' => '启用', 'class_name' => 'layui-btn-success', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 7, 'uri' => '/account/enables', 'menu_no' => 'account-enable'],
                            ['name' => '禁用', 'class_name' => 'layui-btn-danger', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 9, 'uri' => '/account/disables', 'menu_no' => 'account-disable'],
                            ['name' => '重置密码', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 10, 'uri' => '/account/resetPwds', 'menu_no' => 'account-reset-pwds'],
                        ]
                    ],
                    [
                        'name'     => '角色管理', 'icon' => '', 'type' => 1, 'sort' => 2, 'uri' => '/role/index', 'menu_no' => 'role-index',
                        'children' => [
                            ['name' => '查询', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-search', 'type' => 2, 'btn_type' => 2, 'sort' => 1, 'uri' => '/role/list', 'menu_no' => 'role-query'],
                            ['name' => '新增', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-add-1', 'type' => 2, 'btn_type' => 4, 'sort' => 2, 'uri' => '/role/editAdd', 'menu_no' => 'role-add'],
                            ['name' => '新增保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 3, 'uri' => '/role/save', 'menu_no' => 'role-add-save'],
                            ['name' => '编辑', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-edit', 'type' => 2, 'btn_type' => 4, 'sort' => 4, 'uri' => '/role/editAdd', 'menu_no' => 'role-edit'],
                            ['name' => '编辑保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 5, 'uri' => '/role/save', 'menu_no' => 'role-edit-save'],
                            ['name' => '删除', 'class_name' => 'layui-btn-danger', 'icon' => 'layui-icon-delete', 'type' => 2, 'btn_type' => 1, 'sort' => 6, 'uri' => '/role/deletes', 'menu_no' => 'role-delete'],
                            ['name' => '批量删除', 'class_name' => 'layui-btn-danger', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 7, 'uri' => '/role/deletes', 'menu_no' => 'role-deletes'],
                        ]
                    ],
                ]
            ],
            [
                'name'     => '系统管理', 'menu_no' => 'system', 'icon' => 'layui-icon-set', 'type' => 1, 'sort' => 7, 'uri' => '',
                'children' => [
                    [
                        'name'     => '协议列表', 'icon' => '', 'type' => 1, 'sort' => 1, 'uri' => '/agreement/index', 'menu_no' => 'agreement-index',
                        'children' => [
                            ['name' => '查询', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-search', 'type' => 2, 'btn_type' => 2, 'sort' => 1, 'uri' => '/agreement/list', 'menu_no' => 'agreement-query'],
                            ['name' => '编辑', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-edit', 'type' => 2, 'btn_type' => 4, 'sort' => 3, 'uri' => '/agreement/editAdd', 'menu_no' => 'agreement-edit'],
                            ['name' => '编辑保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 4, 'uri' => '/agreement/update', 'menu_no' => 'agreement-edit-save'],
                        ]
                    ],
                    [
                        'name'     => '操作日志', 'icon' => '', 'type' => 1, 'sort' => 2, 'uri' => '/operateLog/index', 'menu_no' => 'operateLog-index',
                        'children' => [
                            ['name' => '查询', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-search', 'type' => 2, 'btn_type' => 2, 'sort' => 1, 'uri' => '/operateLog/list', 'menu_no' => 'operateLog-query'],
                        ]
                    ],
                    [
                        'name'     => '广告管理', 'icon' => '', 'type' => 1, 'sort' => 3, 'uri' => '/banner/index', 'menu_no' => 'banner-index',
                        'children' => [
                            ['name' => '查询', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-search', 'type' => 2, 'btn_type' => 2, 'sort' => 1, 'uri' => '/banner/list', 'menu_no' => 'banner-query'],
                            ['name' => '新增', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-add-1', 'type' => 2, 'btn_type' => 4, 'sort' => 2, 'uri' => '/banner/editAdd', 'menu_no' => 'banner-add'],
                            ['name' => '新增保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 3, 'uri' => '/banner/create', 'menu_no' => 'banner-add-save'],
                            ['name' => '编辑', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-edit', 'type' => 2, 'btn_type' => 4, 'sort' => 3, 'uri' => '/banner/editAdd', 'menu_no' => 'banner-edit'],
                            ['name' => '编辑保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 4, 'uri' => '/banner/update', 'menu_no' => 'banner-edit-save'],
                            ['name' => '删除', 'class_name' => 'layui-btn-danger', 'icon' => 'layui-icon-delete', 'type' => 2, 'btn_type' => 1, 'sort' => 5, 'uri' => '/banner/deletes', 'menu_no' => 'banner-delete'],
                            ['name' => '批量删除', 'class_name' => 'layui-btn-danger', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 6, 'uri' => '/banner/deletes', 'menu_no' => 'banner-deletes'],
                            ['name' => '启用', 'class_name' => 'layui-btn-success', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 7, 'uri' => '/banner/enables', 'menu_no' => 'banner-enable'],
                            ['name' => '禁用', 'class_name' => 'layui-btn-danger', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 9, 'uri' => '/banner/disables', 'menu_no' => 'banner-disable'],
                        ]
                    ],
                    [
                        'name'     => '权限资源', 'icon' => '', 'type' => 1, 'sort' => 3, 'uri' => '/menu/index', 'menu_no' => 'menu-index',
                        'children' => [
                            ['name' => '查询', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-search', 'type' => 2, 'btn_type' => 2, 'sort' => 1, 'uri' => '/menu/list', 'menu_no' => 'menu-query'],
                            ['name' => '新增', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-add-1', 'type' => 2, 'btn_type' => 4, 'sort' => 2, 'uri' => '/menu/editAdd', 'menu_no' => 'menu-add'],
                            ['name' => '新增保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 3, 'uri' => '/menu/create', 'menu_no' => 'menu-add-save'],
                            ['name' => '编辑', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-edit', 'type' => 2, 'btn_type' => 4, 'sort' => 3, 'uri' => '/menu/editAdd', 'menu_no' => 'menu-edit'],
                            ['name' => '编辑保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 4, 'uri' => '/menu/update', 'menu_no' => 'menu-edit-save'],
                            ['name' => '删除', 'class_name' => 'layui-btn-danger', 'icon' => 'layui-icon-delete', 'type' => 2, 'btn_type' => 1, 'sort' => 5, 'uri' => '/menu/deletes', 'menu_no' => 'menu-delete'],
                            ['name' => '批量删除', 'class_name' => 'layui-btn-danger', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 6, 'uri' => '/menu/deletes', 'menu_no' => 'menu-deletes'],
                       ]
                    ],
                    [
                        'name'     => '系统配置', 'icon' => '', 'type' => 1, 'sort' => 8, 'uri' => '/systemConfig/index', 'menu_no' => 'systemConfig-index',
                        'children' => [
                            ['name' => '查询', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-search', 'type' => 2, 'btn_type' => 2, 'sort' => 1, 'uri' => '/systemConfig/list', 'menu_no' => 'systemConfig-query'],
                            ['name' => '新增', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-add-1', 'type' => 2, 'btn_type' => 4, 'sort' => 2, 'uri' => '/systemConfig/editAdd', 'menu_no' => 'systemConfig-add'],
                            ['name' => '新增保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 3, 'uri' => '/systemConfig/create', 'menu_no' => 'systemConfig-add-save'],
                            ['name' => '编辑', 'class_name' => 'layui-btn', 'icon' => 'layui-icon-edit', 'type' => 2, 'btn_type' => 4, 'sort' => 3, 'uri' => '/systemConfig/editAdd', 'menu_no' => 'systemConfig-edit'],
                            ['name' => '编辑保存', 'class_name' => 'layui-btn', 'icon' => '', 'type' => 2, 'btn_type' => 5, 'sort' => 4, 'uri' => '/systemConfig/update', 'menu_no' => 'systemConfig-edit-save'],
                            ['name' => '删除', 'class_name' => 'layui-btn-danger', 'icon' => 'layui-icon-delete', 'type' => 2, 'btn_type' => 1, 'sort' => 5, 'uri' => '/systemConfig/deletes', 'menu_no' => 'systemConfig-delete'],
                            ['name' => '批量删除', 'class_name' => 'layui-btn-danger', 'icon' => '', 'type' => 2, 'btn_type' => 1, 'sort' => 6, 'uri' => '/systemConfig/deletes', 'menu_no' => 'systemConfig-deletes'],
                        ]
                    ],
                ]
            ],
        ];
        \App\Models\AdminMenu::truncate();
        foreach ($data as $k => $v) {
            $this->create($v, 0, 1);
        }
    }

    public function create($data, $pid = 0, $level = 1)
    {
        $data['pid']      = $pid;
        $data['uri']      = '/admin' . $data['uri'];
        $data['level']    = $level;
        $data['is_win']   = $level == 2 ? 1 : 0;
        $data['btn_type'] = $data['btn_type'] ?? 0;
        $children         = $data['children'] ?? null;
        unset($data['children']);
        $menu = \App\Models\AdminMenu::create($data);
        if ($children) {
            foreach ($children as $k => $v) {
                $this->create($v, $menu->id, $menu->level + 1);
            }
        }
    }
}
